package com.fankui.serviceedu.controller.front;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fankui.commonutils.R;
import com.fankui.serviceedu.entity.EduCourse;
import com.fankui.serviceedu.entity.Teacher;
import com.fankui.serviceedu.entity.enums.CourseStatusEnum;
import com.fankui.serviceedu.service.EduCourseService;
import com.fankui.serviceedu.service.TeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName EduIndexFrontController
 * Description TODO
 * Date 2023/11/30 17:47
 *
 * @Author 范奎
 * @Since JDK 11
 */
@Api(description = "前台首页数据")
@RestController
@RequestMapping("/eduservice/front/index")
public class EduIndexFrontController {

    @Autowired
    private EduCourseService courseService;

    @Autowired
    private TeacherService teacherService;

    @ApiOperation(value = "查询首页课程和讲师数据")
    @Cacheable(value = "courseAndTeacher", key = "'selectIndexList'")
    @GetMapping("getCourseAndTeacher")
    public R getCourseAndTeacher() {
        // 查询8条热门课程数据
        QueryWrapper<EduCourse> courseWrapper = new QueryWrapper<>();
        courseWrapper.orderByDesc("id");
        courseWrapper.last("limit 8");
        courseWrapper.eq("status", CourseStatusEnum.NORMAL);
        List<EduCourse> courseList = courseService.list(courseWrapper);
        // 查询4条名师数据
        QueryWrapper<Teacher> teacherWrapper = new QueryWrapper<>();
        teacherWrapper.orderByDesc("id");
        teacherWrapper.last("limit 4");
        List<Teacher> teacherList = teacherService.list(teacherWrapper);
        Map<String, Object> map = new HashMap<>();
        map.put("courseList", courseList);
        map.put("teacherList", teacherList);
        return R.ok().data(map);
    }
}
